package zzjson.rabbitmq.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import zzjson.rabbitmq.BaseRabbitMq;

import java.io.IOException;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : zzjson.rabbitmq.confirm</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年04月29日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class BatchConfirmProducer {
	private final static String QUEUE_NAME = "ORIGIN_QUEUE";

	public static void main(String[] args) throws IOException {
		Connection conn = BaseRabbitMq.getConn();
		Channel channel = conn.createChannel();

		String msg = "Hello world, Rabbit MQ ,Batch Confirm";
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		try {
			channel.confirmSelect();
			for (int i = 0; i < 5; i++) {

				// 发送消息
				channel.basicPublish("", QUEUE_NAME, null, (msg + "-" + i).getBytes());

				// 批量确认结果，ACK如果是Multiple=True，代表ACK里面的Delivery-Tag之前的消息都被确认了
				// 比如5条消息可能只收到1个ACK，也可能收到2个（抓包才看得到）
				// 直到所有信息都发布，只要有一个未被Broker确认就会IOException
				channel.waitForConfirmsOrDie();
				System.out.println("消息发送完毕，批量确认成功");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		BaseRabbitMq.close(channel, conn);
	}
}